﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using BudgetApp.Adapters;
using BudgetApp.Data;
using BudgetApp.Data.Models;
using HelpDeskTicketingApp.Models;
using Microsoft.AspNet.Identity;

namespace BudgetApp.Controllers
{
    [Authorize]
    public class BudgetingController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();
        private IBudgetAdapter _budgetAdapter;

        public BudgetingController()
        {
            _budgetAdapter = BudgetAdapterFactory.GetBudgetAdapter();
        }

        public BudgetingController(IBudgetAdapter budgetAdapter)
        {
            _budgetAdapter = budgetAdapter;
        }

        // GET: Budget
        public ActionResult Index()
        {
            var trackBills =
                db.TrackBills.Include(t => t.Category)
                    .Include(t => t.InputType)
                    .Include(t => t.SubCategory)
                    .Include(t => t.User);
            
            return View(trackBills.ToList());
        }
        
        // GET: Budget/Details/id
        public ActionResult Details(int id)
        {
            var trackBill = _budgetAdapter.GetBillDetails(id);
            //TrackBill trackBill = db.TrackBills.Find(id);
            if (trackBill == null)
            {
                return HttpNotFound();
            }
            return View(trackBill);
        }

        // GET: Budget/Create
        public ActionResult Create()
        {
            ViewBag.CategoryType = new SelectList(db.Categories, "CategoryId", "CategoryType");
            ViewBag.Type = new SelectList(db.InputTypes, "TypeId", "Type");
            ViewBag.SubCategoryType = new SelectList(db.SubCategories, "SubCategoryId", "SubCategoryType");
            return View();
        }

        // POST: Budget/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "BillId,DateDue,Amount,Description,CategoryType,SubCategoryType,Type,UserId")] TrackBill trackBill)
        {
            //trackBill.UserId = User.Identity.GetUserId();
            //trackBill.User = _budgetAdapter.GetUserById(trackBill.UserId);
            //string userEmail = trackBill.User.Email;
            //string userName = trackBill.User.FirstName + " " + trackBill.User.LastName;

            if (ModelState.IsValid)
            {
                //EmailManager.SendNewEmail(userName, userEmail, trackBill.Description, trackBill.DateDue, trackBill.Amount);

                db.TrackBills.Add(trackBill);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CategoryType = new SelectList(db.Categories, "CategoryId", "CategoryType", trackBill.CategoryType);
            ViewBag.Type = new SelectList(db.InputTypes, "TypeId", "Type", trackBill.Type);
            ViewBag.SubCategoryType = new SelectList(db.SubCategories, "SubCategoryId", "SubCategoryType", trackBill.SubCategoryType);
            return View(trackBill);
        }

        // GET: Budget/Edit/id
        public ActionResult Edit(int id)
        {
            var trackBill = _budgetAdapter.GetBillDetails(id);
            if (trackBill == null)
            {
                return HttpNotFound();
            }
            ViewBag.CategoryType = new SelectList(db.Categories, "CategoryId", "CategoryType");
            ViewBag.Type = new SelectList(db.InputTypes, "TypeId", "Type");
            ViewBag.SubCategoryType = new SelectList(db.SubCategories, "SubCategoryId", "SubCategoryType");
            return View(trackBill);
        }

        // POST: Budget/Edit/id
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "BillId,DateDue,Amount,Description,CategoryType,SubCategoryType,Type,UserId")] TrackBill trackBill)
        {
            if (ModelState.IsValid)
            {
                db.Entry(trackBill).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CategoryType = new SelectList(db.Categories, "CategoryId", "CategoryType", trackBill.CategoryType);
            ViewBag.Type = new SelectList(db.InputTypes, "TypeId", "Type", trackBill.Type);
            ViewBag.SubCategoryType = new SelectList(db.SubCategories, "SubCategoryId", "SubCategoryType", trackBill.SubCategoryType);
            return View(trackBill);
        }

        // GET: Budget/Delete/id
        public ActionResult Delete(int id)
        {
            var trackBill = _budgetAdapter.GetBillDetails(id);
            if (trackBill == null)
            {
                return HttpNotFound();
            }
            return View(trackBill);
        }

        // POST: Budget/Delete/id
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            var trackBill = _budgetAdapter.GetBillDetails(id);
            db.TrackBills.Remove(trackBill);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
